package cn.gson.crm.controller;

import cn.gson.crm.model.pojos.*;
import cn.gson.crm.model.service.Cpcgservice;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/cpcg")
public class CpcgController {
    @Autowired
    Cpcgservice cpcgservice;
    @RequestMapping("/cpcg")
    @ResponseBody
    public Map<String,Object> cpp(Integer currentPage, Integer pageSize, String name, String start,String end){
        if(currentPage==null || currentPage==' '||pageSize==null||pageSize==' '){
            currentPage=1;
            pageSize=5;
        }
        System.err.println("currentPage:"+currentPage);
        System.err.println("pageSize:"+pageSize);
        Page<Object> obj = PageHelper.startPage(currentPage,pageSize);
        System.err.println("开始时间："+start);
        System.err.println("结束时间:"+end);


        String datas = null;
        String dates1 = null;
        Date time;
        Date times;
        if(start != null){
            start = start.replace("Z", " UTC");
        }
        if(end != null){
            end = end.replace("Z", " UTC");
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if(start != null){
                time = format.parse(start);
                String result = defaultFormat.format(time);
                datas = result;
                System.out.println("开始"+datas);
            }
            if(end != null) {
                times = format.parse(end);
                String results = defaultFormat.format(times);
                dates1 = results;
                System.out.println("结束"+dates1);
            }
        } catch (Exception e) {
            time = null;
            times = null;
        }
        System.err.println(name);
        List<Purchasexq> cpp = cpcgservice.cpcg(name,datas,dates1);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("row",cpp);
        return map;
    }

    //采购新增
    @RequestMapping("/cgxz")
    @ResponseBody
    public void cg(String positionList){
        cpcgservice.cg(positionList);
    }

    //查询采购名称
    @RequestMapping("/cgm")
    @ResponseBody
    public List<Pro> cgm(){
        return cpcgservice.cgmc();
    }
    //查询供货商
    @RequestMapping("/ghs")
    @ResponseBody
    public List<Supplier> ghs(){
        return cpcgservice.ghs();
    }

    //根据供货商名查询联系电话
    @RequestMapping("/ghsm")
    @ResponseBody
    public List<Supplier> gh(String ghsm){
        return cpcgservice.gh(ghsm);
    }

    //根据供货商联系人查询联系电话
    @RequestMapping("/dianhua")
    @ResponseBody
    public List<Supplier> gh1(String xi){
        return cpcgservice.dian(xi);
    }

    @RequestMapping("/dh")
    @ResponseBody
    public String getOrderIdByTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
//        Random random=new Random();
//        for(int i=0;i<3;i++){
//            result+=random.nextInt(10);
//        }
        String dh="cg"+newDate;
        return dh;
    }

    @RequestMapping("/tjcp")
    @ResponseBody
    public List<SpecifValue> tjcp(){
        return cpcgservice.tjcp();
    }

    @RequestMapping("/mccx")
    @ResponseBody
    public List<ProductXq> mccx(){
        return cpcgservice.cpmc();
    }

    @RequestMapping("/cgid")
    @ResponseBody
    public List<Purchasexq> cgid(Integer cgid){
        return cpcgservice.cgid(cgid);
    }

    @RequestMapping("/chanpinid")
    @ResponseBody
    public List<Pro> chanpinid(String chanpinid){
        return cpcgservice.chanpinid(chanpinid);
    }

    @RequestMapping("/cgmx1")
    @ResponseBody
    public List<Purchasexq> cgmx1(Integer caigouid){
        return cpcgservice.cgmx1(caigouid);
    }


    @RequestMapping("/user")
    @ResponseBody
    public List<User> user(Integer caigouid){
        return cpcgservice.user();
    }
}
